<?php

class IndexController extends Zend_Controller_Action
{
	private $mailQueue;
	private $user;
	private $message;

    public function init()
    {
        $this->mailQueue = new MailQueue ( );
		$this->user = new User();
		$this->message = new Message();
		$this->view->pageLogo="big";
		
		$this->initView();
		$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');		 
		$this->view->flashmsgs = $this->_flashMessenger->getMessages();
    }

    public function indexAction()
    {
    	$config = Zend_Registry::get("config");
    	if (!$this->_helper->user->isLoggedIn()) {
    		$this->_redirect($config->baseurl."/user/login");
    	} else {
    		$this->_redirect($config->baseurl."/bulletin/list");
    	}
    	
//    	echo $this->_helper->location->countryCityFromIP("83.179.39.242");
    	include("geoip.inc");

    	$config = Zend_Registry::get("config");
    	
		$gi = geoip_open($config->geoipdata->file,GEOIP_STANDARD);
		
		echo geoip_country_code_by_addr($gi, "83.179.39.242") . "\t" .
		     geoip_country_name_by_addr($gi, "83.179.39.242") . "\n";
		echo geoip_country_code_by_addr($gi, "80.24.24.24") . "\t" .
		     geoip_country_name_by_addr($gi, "80.24.24.24") . "\n";
		echo "dsa";
		geoip_close($gi);
		exit;
        // action body
    }

    public function aboutAction()
    {
        // action body
    }
    public function termsAction()
    {
        // action body
    }
    public function helpAction()
    {
        // action body
    }
    public function contactAction() {
    	$config = Zend_Registry::get ( 'config' );
    	$versionNm = $config->project->version;
		$this->view->headScript ()->appendFile ( $config->baseurl . '/'.$versionNm.'_js/jquery.validate.min.js' );
		$this->view->headScript ()->appendFile ( $config->baseurl . '/'.$versionNm.'_js/contact.js' );
		$form = new ContactUs ( );
		$config = Zend_Registry::get("config");
		$this->view->form = $form;
		if ($this->getRequest ()->isPost ()) {
			if ($form->isValid ( $_POST )) {
//				$isValid = $this->_helper->common->validReCaptcha($this->_getAllParams());
//				
//				if ($isValid) {
					$name = $form->getValue ( 'name' );
					$email = $form->getValue ( 'email' );
					$body = nl2br ( $form->getValue ( 'body' ) );
					
					$db = Zend_Registry::get ( 'db' );
					$db->beginTransaction ();
					$this->mailQueue->addToQueue ( MailType::CONTACT_US, $name, '联系我们', $email, Constant::CONTACT_MAIL, $body, $this->_helper->generator->generateCurrentTime () );
					$db->commit ();
					$this->_flashMessenger->addMessage("您的留言已经发送，我们会尽快处理并与您取得联系！谢谢您对我们的支持！！");
					$this->_redirect ( $config->baseurl.'/contact' );
//				} else {
//					throw new Exception('验证码错误!');
//				}
			}
		}
    }

}

